//
//  YDYNetWorkTool.m
//  DY_Bilibili
//
//  Created by 杨德勇 on 2016/10/9.
//  Copyright © 2016年 杨德勇. All rights reserved.
//

#import "YDYNetWorkTool.h"
#import "YDYHttpSessionManager.h"
@implementation YDYNetWorkTool
+ (nullable NSURLSessionDataTask *)GET:(NSString *)URLString
                            parameters:(nullable id)parameters
                              progress:(nullable void (^)(NSProgress *downloadProgress))downloadProgress
                               success:(nullable void (^)(NSURLSessionDataTask *task, id _Nullable responseObject))success
                               failure:(nullable void (^)(NSURLSessionDataTask * _Nullable task, NSError *error))failure{
    
    YDYHttpSessionManager *manager = [YDYHttpSessionManager shareManager];
    NSURLSessionDataTask *task = [manager GET:URLString parameters:parameters progress:downloadProgress success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        success(task,responseObject);
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        failure(task,error);
    }];;
    return task;
}


+ (nullable NSURLSessionDataTask *)POST:(NSString *)URLString
                             parameters:(nullable id)parameters
                               progress:(nullable void (^)(NSProgress *uploadProgress))uploadProgress
                                success:(nullable void (^)(NSURLSessionDataTask *task, id _Nullable responseObject))success
                                failure:(nullable void (^)(NSURLSessionDataTask * _Nullable task, NSError *error))failure{
    
    YDYHttpSessionManager* manager = [YDYHttpSessionManager shareManager];
    NSURLSessionDataTask *task = [manager POST:URLString parameters:parameters progress:uploadProgress success:success failure:failure];
    
    return task;
    
}
+ (void)cancel{
    // 取消网络请求
    [[YDYHttpSessionManager shareManager].operationQueue cancelAllOperations];
    
    // 取消任务中的所有网络请求
    //    [[YDYHttpSessionManager shareManager].tasks makeObjectsPerformSelector:@selector(cancel)];
    
    // 杀死Session
    //    [[YDYHttpSessionManager shareManager] invalidateSessionCancelingTasks:YES];
}
@end
